package persistency.writer;

import java.util.Iterator;

import model.Course;
import model.OfferedCourse;
import model.Registration;
import settings.PathSettings;

public class CourseWriter extends ContainerWriter{
	
	
	private Writer offeredCourseWriter = new OfferedCourseWriter();
	private IntersectionWriter courseRequiredcourseWriter = new CourseRequiredCourseWriter();
	private IntersectionWriter courseOfferedCourseWriter = new CourseOfferedCourseWriter();
	
	/**
	 * Instantiate the CourseWriter object
	 */
	public CourseWriter(){
		super(PathSettings.COURSEFILE,"#Code\tName\tMinutes\tCredits\n");
	}

	@Override
	public void write(Writable writable) {
		
		super.write(writable);
		Course course = (Course) writable;
		
		Iterator<OfferedCourse> offeredCourseIterator = course.getOfferedcourses().iterator();
		while (offeredCourseIterator.hasNext()) {
			OfferedCourse offeredCourse = offeredCourseIterator.next();
			courseOfferedCourseWriter.write(course.getPrimaryKey(), offeredCourse.getPrimaryKey());
			offeredCourseWriter.write(offeredCourse);
		}
		
		courseRequiredcourseWriter.write(course.getCode(),course.getRequiredCourses());
		
	}
	
	/**
	 * Inner class that handles the intersection of Course and OfferedCourse
	 * @author Wim
	 *
	 */
	private class CourseOfferedCourseWriter extends IntersectionWriter {

		public CourseOfferedCourseWriter(){
			super(PathSettings.COURSEOFFEREDCOURSEFILE,"#CourseCode\tOfferedCourseKey\n");
		}
	}
	
	/**
	 * Inner class that handles the OfferedCourses
	 * @author Wim
	 *
	 */
	private class OfferedCourseWriter extends Writer {
		
		private Writer registrationWriter = new RegistrationWriter();
		private IntersectionWriter offeredcourseRegistrationWriter = new OfferedCourseRegistrationWriter();
		
		public OfferedCourseWriter(){
			super(PathSettings.OFFEREDCOURSEFILE,"#OfferedCourseKey\tCourseCode\tClassroom\tTeacher\tNumberOfPlaces\tDay\tStartHour\tStartMinutes\tDuration\tState\n");
		}
		
		@Override
		public void write(Writable writable) {
			
			super.write(writable.getPrimaryKey() + "\t" +writable.toDataString());
			OfferedCourse offeredCourse = (OfferedCourse) writable;
			
			Iterator<? extends Writable> writableIterator = offeredCourse.getRegistrations().iterator();
			while (writableIterator.hasNext()) {
				Writable registration = writableIterator.next();
				offeredcourseRegistrationWriter.write(offeredCourse.getPrimaryKey(), registration.getPrimaryKey()+"\t"+((Registration)registration).isReceiveCourseEmail() );
				registrationWriter.write(registration);
			}
			
		}
		
		@Override
		public void close(){
			super.close();
			offeredcourseRegistrationWriter.close();
			registrationWriter.close();
		}
		
		
		/**
		 * Inner class that handles the Registrations
		 * @author Wim
		 *
		 */
		private class RegistrationWriter extends Writer {
			
			public RegistrationWriter(){
				super(PathSettings.REGISTRATIONFILE,"#RegistrationKey\tStudentNumber\tStartDate\n");
			}
			
			@Override
			public void write(Writable writable) {
				super.write(writable.getPrimaryKey() + "\t" + writable.toDataString());
			}
		}
		
		/**
		 * Inner class that handles the relation between OfferedCourse and Registration
		 * @author Wim
		 *
		 */
		private class OfferedCourseRegistrationWriter extends IntersectionWriter {
			public OfferedCourseRegistrationWriter(){
				super(PathSettings.OFFEREDCOURSEREGISTRATIONFILE,"#OfferedCourseKey\tRegistrationKey\trecieveCourseEmail\n");
			}
		}
	}
	
	/**
	 * Inner class that handles the intersection of Course and RequiredCourse
	 * @author Wim
	 *
	 */
	private class CourseRequiredCourseWriter extends IntersectionWriter {

		public CourseRequiredCourseWriter(){
			super(PathSettings.REQUIREDCOURSEFILE,"#CourseCode\tRequiredCourseCode\n");
		}
		
		
	}
	@Override
	public void close(){
		super.close();
		offeredCourseWriter.close();
		courseRequiredcourseWriter.close();
		courseOfferedCourseWriter.close();
	}
	
}
